package com.kchouse.myadapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.maps2d.AMapUtils;
import com.amap.api.maps2d.model.LatLng;
import com.kchouse.R;
import com.kchouse.StartActivity;
import com.kchouse.bean.HostItem;
import com.kchouse.bean.JobItem;
import com.squareup.picasso.Picasso;

import java.text.DecimalFormat;
import java.util.ArrayList;

public class JobAdapter extends RecyclerView.Adapter<JobAdapter.ViewHolder> {

    private ArrayList<JobItem> datas;
    private Context context;


    public JobAdapter(Context context, ArrayList<JobItem> datas) {
        this.datas = datas;
        this.context = context;
    }

    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {

        View view = null;
        ViewHolder vh = null;
        view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.lv_recruitment_item, viewGroup, false);
        vh = new ViewHolder(view);
        return vh;
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {


        final JobItem item = datas.get(position);
        viewHolder.tv_num.setText("人数：" + item.getNum()+"名");
        viewHolder.tv_salary.setText(item.getSalary()+"-"+item.getTop());
        viewHolder.tv_merchantName.setText(item.getMerchantShortName());
        viewHolder.tv_area.setText(item.getArea());
//
//        if (item.isFollowStatus()) {
//            viewHolder.tv_merchantName.setTextColor(context.getResources().getColor(R.color.text_main));
//            viewHolder.tv_merchantName.setTypeface(Typeface.DEFAULT_BOLD);
//
//        } else {
//            viewHolder.tv_merchantName.setTypeface(Typeface.DEFAULT);
//            viewHolder.tv_merchantName.setTextColor(context.getResources().getColor(R.color.colorblack));
//        }
        double distance = item.getDistance() / 1000;
        DecimalFormat df = new DecimalFormat("0.##");
        String st = df.format(distance);
        viewHolder.tv_distance.setText(st + "km");

        viewHolder.bt_contactMobile.setOnClickListener(new CallOnChick(item));

    }


    class CallOnChick implements OnClickListener{

        private  JobItem item;

        public  CallOnChick(JobItem item){

            this.item=item;
        }

        @Override
        public void onClick(View v) {
            callPhone(item.getContactMobile());
        }
    }

    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    public void callPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        context.startActivity(intent);
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return datas.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView tv_num;
        public TextView tv_salary;
        public TextView tv_merchantName;
        public TextView tv_area;
        public TextView tv_distance;
        public Button bt_contactMobile;

        public ViewHolder(View view) {
            super(view);
            tv_salary = (TextView) view.findViewById(R.id.tv_salary);
            tv_num = (TextView) view.findViewById(R.id.tv_num);
            tv_distance = (TextView) view.findViewById(R.id.tv_distance);
            tv_merchantName = (TextView) view.findViewById(R.id.tv_merchantName);
            tv_area = (TextView) view.findViewById(R.id.tv_area);
            bt_contactMobile = (Button) view.findViewById(R.id.bt_contactMobile);
        }
    }


}
